<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>发布—订阅模式 | Freeisms</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
    <meta name="description" content="记录个人前端技术成长点滴">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    
    <link rel="preload" href="/assets/css/0.styles.9efbc543.css" as="style"><link rel="preload" href="/assets/js/app.e45e7f79.js" as="script"><link rel="preload" href="/assets/js/4.1dc261f9.js" as="script"><link rel="preload" href="/assets/js/26.6975fab4.js" as="script"><link rel="prefetch" href="/assets/js/10.910e44c4.js"><link rel="prefetch" href="/assets/js/11.3cb7fbc0.js"><link rel="prefetch" href="/assets/js/12.e84e4441.js"><link rel="prefetch" href="/assets/js/13.a20b1e0e.js"><link rel="prefetch" href="/assets/js/14.5bb81eb0.js"><link rel="prefetch" href="/assets/js/15.9ac9c7b5.js"><link rel="prefetch" href="/assets/js/16.e65d6e8b.js"><link rel="prefetch" href="/assets/js/17.3bcb37e9.js"><link rel="prefetch" href="/assets/js/18.238c27dc.js"><link rel="prefetch" href="/assets/js/19.0bf085fb.js"><link rel="prefetch" href="/assets/js/20.af32ca4b.js"><link rel="prefetch" href="/assets/js/21.d7d0a4bf.js"><link rel="prefetch" href="/assets/js/22.fb0bd41c.js"><link rel="prefetch" href="/assets/js/23.e40c513c.js"><link rel="prefetch" href="/assets/js/24.df877caa.js"><link rel="prefetch" href="/assets/js/25.ff943ee7.js"><link rel="prefetch" href="/assets/js/27.49355cdd.js"><link rel="prefetch" href="/assets/js/28.08ac8c0a.js"><link rel="prefetch" href="/assets/js/29.864244b6.js"><link rel="prefetch" href="/assets/js/30.9fde9cc1.js"><link rel="prefetch" href="/assets/js/31.04c169d6.js"><link rel="prefetch" href="/assets/js/32.57021f58.js"><link rel="prefetch" href="/assets/js/33.09502731.js"><link rel="prefetch" href="/assets/js/34.8a018286.js"><link rel="prefetch" href="/assets/js/35.097f93be.js"><link rel="prefetch" href="/assets/js/36.37ccdfeb.js"><link rel="prefetch" href="/assets/js/37.11976f3e.js"><link rel="prefetch" href="/assets/js/38.98243aad.js"><link rel="prefetch" href="/assets/js/39.a3c78447.js"><link rel="prefetch" href="/assets/js/40.7be6398d.js"><link rel="prefetch" href="/assets/js/41.33d492ee.js"><link rel="prefetch" href="/assets/js/42.f1c675e6.js"><link rel="prefetch" href="/assets/js/43.bbda546b.js"><link rel="prefetch" href="/assets/js/44.2dc401cd.js"><link rel="prefetch" href="/assets/js/45.33bd1151.js"><link rel="prefetch" href="/assets/js/46.b6b09012.js"><link rel="prefetch" href="/assets/js/47.55feb448.js"><link rel="prefetch" href="/assets/js/48.fc9dc909.js"><link rel="prefetch" href="/assets/js/49.fd6d33c4.js"><link rel="prefetch" href="/assets/js/5.ac586c16.js"><link rel="prefetch" href="/assets/js/50.8e33b7f9.js"><link rel="prefetch" href="/assets/js/51.fd278c0a.js"><link rel="prefetch" href="/assets/js/52.fbd66a3b.js"><link rel="prefetch" href="/assets/js/53.f829ac68.js"><link rel="prefetch" href="/assets/js/54.7c7e5ec8.js"><link rel="prefetch" href="/assets/js/55.42e87201.js"><link rel="prefetch" href="/assets/js/56.2301ffbd.js"><link rel="prefetch" href="/assets/js/57.6d5ed1a9.js"><link rel="prefetch" href="/assets/js/58.8dc29eab.js"><link rel="prefetch" href="/assets/js/59.722945c8.js"><link rel="prefetch" href="/assets/js/6.64876e20.js"><link rel="prefetch" href="/assets/js/60.c43354e4.js"><link rel="prefetch" href="/assets/js/61.b9d1ad1d.js"><link rel="prefetch" href="/assets/js/62.d61bfc10.js"><link rel="prefetch" href="/assets/js/63.d84a88b5.js"><link rel="prefetch" href="/assets/js/64.0f25c14d.js"><link rel="prefetch" href="/assets/js/7.d74ac93a.js"><link rel="prefetch" href="/assets/js/8.6c8e2482.js"><link rel="prefetch" href="/assets/js/9.085dc649.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.4e554982.js"><link rel="prefetch" href="/assets/js/vendors~notification.4b668bb8.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9efbc543.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Freeisms</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my-docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><a href="/my-docs/design-pattern/" class="nav-link router-link-active">
  设计模式
</a></div><div class="nav-item"><a href="/my-docs/leetcode/" class="nav-link">
  LeetCode
</a></div><div class="nav-item"><a href="/my-docs/library/vue-juejin-comment.html" class="nav-link">
  开源库
</a></div><div class="nav-item"><a href="https://www.striveforus.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  博客
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://github.com/Fengfengfeng-up/personal-docs" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my-docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><a href="/my-docs/design-pattern/" class="nav-link router-link-active">
  设计模式
</a></div><div class="nav-item"><a href="/my-docs/leetcode/" class="nav-link">
  LeetCode
</a></div><div class="nav-item"><a href="/my-docs/library/vue-juejin-comment.html" class="nav-link">
  开源库
</a></div><div class="nav-item"><a href="https://www.striveforus.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  博客
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://github.com/Fengfengfeng-up/personal-docs" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>设计模式</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my-docs/design-pattern/" aria-current="page" class="sidebar-link">写在前面</a></li><li><a href="/my-docs/design-pattern/singleton.html" class="sidebar-link">单例模式</a></li><li><a href="/my-docs/design-pattern/strategy.html" class="sidebar-link">策略模式</a></li><li><a href="/my-docs/design-pattern/proxy.html" class="sidebar-link">代理模式</a></li><li><a href="/my-docs/design-pattern/iterator.html" class="sidebar-link">迭代器模式</a></li><li><a href="/my-docs/design-pattern/pub-sub.html" aria-current="page" class="active sidebar-link">发布—订阅模式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my-docs/design-pattern/pub-sub.html#理解发布-订阅模式" class="sidebar-link">理解发布—订阅模式</a></li><li class="sidebar-sub-header"><a href="/my-docs/design-pattern/pub-sub.html#javascript-中的发布-订阅模式" class="sidebar-link">JavaScript 中的发布—订阅模式</a></li></ul></li><li><a href="/my-docs/design-pattern/command.html" class="sidebar-link">命令模式</a></li><li><a href="/my-docs/design-pattern/composite.html" class="sidebar-link">组合模式</a></li><li><a href="/my-docs/design-pattern/template-method.html" class="sidebar-link">模板方法模式</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="发布-订阅模式"><a href="#发布-订阅模式" class="header-anchor">#</a> 发布—订阅模式</h1> <p><strong>发布—订阅模式</strong>类似观察者模式，它定义了对象之间的一种一对多的依赖关系，当一个对象的状态改变时，所有依赖于它的对象都将得到通知。</p> <h2 id="理解发布-订阅模式"><a href="#理解发布-订阅模式" class="header-anchor">#</a> 理解发布—订阅模式</h2> <p>从字面上不难看出，发布—订阅模式有“发布”和“订阅”两个动词对应着发布者和订阅者两种对象，而订阅者往往是多个，所以也就是一对多的关系。当发布者达到某种状态时，将执行“发布”动作，订阅者们也将收到“发布”的消息。</p> <p>在现实生活中，随处可见发布—订阅模式的例子。比如微信公众号，人们通过订阅喜欢的公众号来获取信息，当公众号管理者发布新文章时，微信后台将陆续通知订阅该公众号的所有读者。公众号管理者不需要自己主动通知所有的读者，只需要点击发布按钮即可，读者们也不需要一直盯着屏幕查看是否更新，因为公众号一旦更新，微信会立刻发出提醒通知。公众号管理者也不用担心新增的读者是否能收到更新消息，因为微信后台会将一切搞定。</p> <p>由上面的例子可以得出以下两点：</p> <ul><li>发布—订阅模式使得我们无需关心一个对象内部状态的改变，只需要订阅感兴趣的某个事件发生点即可，即为时间上的解耦。</li> <li>发布—订阅模式可以取代对象之间硬编码的通知机制，一个对象不需要再显式地调用另一个对象的某个接口，即为对象之间的解耦。</li></ul> <h2 id="javascript-中的发布-订阅模式"><a href="#javascript-中的发布-订阅模式" class="header-anchor">#</a> JavaScript 中的发布—订阅模式</h2> <p>在 JavaScript 中也能轻松找到发布—订阅模式的影子，例如 DOM 事件：</p> <div class="language-js extra-class"><pre class="language-js"><code>document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'body'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>因为无法预测用户动作，所以我们订阅 <code>document.body</code> 上的点击事件，当事件发生时，引擎会自动执行我们写入的回调函数。</p> <h3 id="实现发布-订阅模式"><a href="#实现发布-订阅模式" class="header-anchor">#</a> 实现发布—订阅模式</h3> <p>假如有这样一个需求，要求实现一个 <code>Events</code> 模块，可以实现自定义事件的订阅、触发、移除等功能。</p> <p>使用该功能的代码如下：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> event <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Events</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> <span class="token function-variable function">fn1</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'I want sleep1'</span><span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span>
<span class="token keyword">const</span> <span class="token function-variable function">fn2</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'I want sleep2'</span><span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span>
event<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> fn1<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
event<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> fn2<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
event<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span>
<span class="token comment">// I want sleep1 1 2 3 4 5 6</span>
<span class="token comment">// I want sleep2 1 2 3 4 5 6</span>
event<span class="token punctuation">.</span><span class="token function">off</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> fn1<span class="token punctuation">)</span>
event<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
<span class="token comment">// I want sleep2 1 2 3 4</span>
event<span class="token punctuation">.</span><span class="token function">once</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'I want sleep too'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
event<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">)</span>
<span class="token comment">// I want sleep2 1 2 3</span>
<span class="token comment">// I want sleep too</span>
event<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span><span class="token string">'sleep'</span><span class="token punctuation">)</span> 
<span class="token comment">// I want sleep2 1 2 3</span>
</code></pre></div><p>使用发布—订阅模式实现该需求的代码如下：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">class</span> <span class="token class-name">Events</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>clients <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// 存放事件类型以及对应的订阅者列表</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// 静态方法，增加订阅者</span>
  <span class="token keyword">static</span> <span class="token function">addListener</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span> listener</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>clients<span class="token punctuation">[</span>type<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>clients<span class="token punctuation">[</span>type<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span> origin<span class="token punctuation">,</span> <span class="token operator">...</span>rest <span class="token punctuation">}</span> <span class="token operator">=</span> listener
    <span class="token keyword">this</span><span class="token punctuation">.</span>clients<span class="token punctuation">[</span>type<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>origin<span class="token punctuation">,</span> rest<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// 订阅</span>
  <span class="token function">on</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span> fn<span class="token punctuation">,</span> <span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    Events<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> type<span class="token punctuation">,</span> <span class="token punctuation">{</span>
      origin<span class="token operator">:</span> fn<span class="token punctuation">,</span>
      bindFn<span class="token operator">:</span> <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// 触发（发布）</span>
  <span class="token function">fire</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span> <span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> clients <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>clients<span class="token punctuation">[</span>type<span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>clients <span class="token operator">||</span> clients<span class="token punctuation">.</span>size <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token punctuation">[</span>origin<span class="token punctuation">,</span> <span class="token punctuation">{</span> bindFn<span class="token punctuation">,</span> once <span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token keyword">of</span> clients<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">bindFn</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>

      <span class="token keyword">if</span> <span class="token punctuation">(</span>once<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        clients<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>origin<span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// 取消订阅</span>
  <span class="token function">off</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span> fn</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> clients <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>clients<span class="token punctuation">[</span>type<span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>clients <span class="token operator">||</span> clients<span class="token punctuation">.</span>size <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    clients<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// 单次订阅（只触发一次）</span>
  <span class="token function">once</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span> fn<span class="token punctuation">,</span> <span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    Events<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> type<span class="token punctuation">,</span> <span class="token punctuation">{</span>
      origin<span class="token operator">:</span> fn<span class="token punctuation">,</span>
      bindFn<span class="token operator">:</span> <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span><span class="token punctuation">,</span>
      once<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>通过以上例子可以发现，在 JavaScript 中主要采取了回调函数的形式来代替传统的发布—订阅模式，显得更加简单和优雅。其次，我们无需选择使用推模型还是拉模型，当事件发生时，通过 <code>arguments</code> 对象和 <code>Function.prototype.apply()</code> 方法可以很便捷地将更改的状态和数据都推送给订阅者。</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/Fengfengfeng-up/personal-docs/edit/main/docs/my-docs/design-pattern/pub-sub.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">3/3/2021, 1:42:33 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my-docs/design-pattern/iterator.html" class="prev">
        迭代器模式
      </a></span> <span class="next"><a href="/my-docs/design-pattern/command.html">
        命令模式
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.e45e7f79.js" defer></script><script src="/assets/js/4.1dc261f9.js" defer></script><script src="/assets/js/26.6975fab4.js" defer></script>
  </body>
</html>
